<script>
import { cloud } from '@/framework/utils/request'
import {
  deleteAd,
  editAd,
  getAdDetail,
  getAdList,
  getAdvertNameList,
  getGoodsList,
  getMyMallInfo
} from '@/api/mallApi/advert'

export default {
  name: 'advPosShop',
  data() {
    return {
      showAll: true,
      isShow: false,
      isEdit: false,
      goodsList: [],
      spaceNameList: [],
      label: '',
      uploadUrl: `${this.baseURL()}/sysFileInfo/upload?secretFlag=N&fileBucket=mall`,
      form: {
        // 广告名称
        adName: '',
        // 广告位名字
        spaceName: '',
        // 开始时间
        startTime: '',
        // 结束时间
        endTime: ''
      },
      tableData: [{}],
      // 分页数据
      currentPage: 1,
      pageSize: 10,
      total: 0,
      tableLoading: false,
      addShow: false,
      editShow: false,
      addForm: {
        // 广告名称
        adName: '',
        spaceId: '',
        // mark: '',
        adType: '',
        adShowId: '',
        startTime: '',
        endTime: '',
        img: ''
      },
      addRules: {
        adName: [
          { required: true, message: '请输入广告名称', trigger: 'blur' }
        ],
        spaceId: [
          { required: true, message: '请选择广告位', trigger: 'change' }
        ],
        adType: [
          { required: true, message: '请选择广告类型', trigger: 'change' }
        ],
        adShowId: [
          { required: true, message: '请选择广告类型值', trigger: 'change' }
        ],
        startTime: [
          { required: true, message: '请选择开始时间', trigger: 'change' }
        ],
        endTime: [
          { required: true, message: '请选择结束时间', trigger: 'change' }
        ],
        img: [{ required: true, message: '请上传广告图片', trigger: 'change' }]
      },
      advertPosList: []
    }
  },
  computed: {
    word() {
      if (this.showAll == false) {
        // 对文字进行处理
        return '收起'
      } else {
        return '展开'
      }
    }
  },
  created() {
    this.getTableData()
  },
  methods: {
    baseURL() {
      return process.env.VUE_APP_BASE_API + cloud.file
    },
    handleClear() {
      this.$refs.form.resetFields()
    },
    handleEdit(row) {
      this.isEdit = true
      this.addShow = true
      getAdvertNameList().then((res) => {
        if (res.code === '00000') {
          this.spaceNameList = res.data
        }
      })
      getAdDetail({ id: row.id }).then((res) => {
        if (res.code === '00000') {
          this.addForm = res.data
          this.addForm.ids = [res.data.id]
        }
      })
    },
    handleDelete(row) {
      this.$confirm('此操作将永久删除该广告, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          // 删除广告
          deleteAd({ ids: [row.id] })
            .then((res) => {
              if (res.code === '00000') {
                this.$message({
                  type: 'success',
                  message: '删除成功!'
                })
                this.getTableData()
              }
            })
            .catch((err) => {
              this.$message({
                type: 'error',
                message: '删除失败!'
              })
            })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    handleView(index, row) {
      this.addShow = true
      this.isShow = true
      getAdvertNameList().then((res) => {
        if (res.code === '00000') {
          this.spaceNameList = res.data
        }
      })
      getAdDetail({ id: row.id }).then((res) => {
        if (res.code === '00000') {
          this.addForm = res.data
          this.addForm.ids = [res.data.id]
        }
      })
    },
    handleSizeChange(val) {
      this.pageSize = val
      this.getTableData()
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.getTableData()
    },
    getTableData() {
      this.tableLoading = true
      const params = {
        pageSize: this.pageSize,
        pageNo: this.currentPage,
        // 广告名称
        adName: this.form.adName,
        // 广告位名字
        spaceName: this.form.spaceName,
        // 开始时间
        startTime: this.form.startTime,
        // 结束时间
        endTime: this.form.endTime
      }
      getAdList(params)
        .then((res) => {
          if (res.code === '00000') {
            this.tableData = res.data.list
            this.total = parseInt(res.data.total)
            this.tableLoading = false
          }
        })
        .catch((err) => {
          this.tableLoading = false
        })
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url
      this.dialogVisible = true
    },
    handleRemove(file, fileList) {
      console.log(file, fileList)
    },
    handleSuccess(response, file, fileList) {
      // 将fileList赋值给this.fileList
      this.fileList = fileList
      // 将响应数据中的fileUrl赋值给addForm.img
      this.addForm.img = response.data.fileUrl
    },
    handleChange() {
      if (this.addForm.adType === 1) {
        this.label = '商品'
        getGoodsList()
          .then((res) => {
            if (res.code === '00000') {
              this.goodsList = res.data.rows
            }
          })
          .catch((err) => {})
      }
      if (this.addForm.adType === 2) {
        this.label = '店铺'
        getMyMallInfo()
          .then((res) => {
            if (res.code === '00000') {
              console.log('res: ', res)
              // this.goodsList = res.data
            }
          })
          .catch((err) => {})
      }
    },
    handleCancel() {
      this.addForm = {}
      this.$refs.addForm.resetFields()
      this.addShow = false
      this.isShow = false
    },
    handleCommit() {
      if (this.isEdit === true) {
        editAd(this.addForm)
          .then((res) => {
            // 关闭弹窗
            this.addShow = false
            this.isEdit = false
            // 提交成功
            this.$message({
              message: '编辑成功',
              type: 'success'
            })
            // 重置表单
            this.$refs.addForm.resetFields()
            // 重新加载数据
            this.getTableData()
          })
          .catch((err) => {})
      } else {
        // 关闭弹窗
        this.addShow = false
        this.isEdit = false
      }
    },
    handleSwitch(row) {
      const params = {
        ids: [row.id],
        ...row
      }
      editAd(params)
        .then((res) => {
          if (res.code === '00000') {
            this.$message({
              message: '操作成功',
              type: 'success'
            })
          }
          // 重新加载数据
          this.getList()
        })
        .catch((err) => {})
    },
    // 展开收起
    closeSearch() {
      this.showAll = !this.showAll
    }
  }
}
</script>

<template>
  <div>
    <div class="form">
      <div class="formLeft">
        <el-row>
          <el-form :model="form" ref="form" :inline="true"
                   label-width="100px">
            <el-row class="btnRow">
              <el-col :span="6" class="elrow" style="margin-right: 135px">
                <el-form-item prop="spaceName">
                  <p>广告位名:</p>
                  <el-input
                    style="width: 300px"
                    v-model="form.spaceName"
                    placeholder="请输入广告位名"
                  ></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="6" class="elrow">
                <el-form-item prop="adName">
                  <p>广告名称:</p>
                  <el-input
                    style="width: 300px"
                    v-model="form.adName"
                    placeholder="请输入广告名称:"
                  ></el-input>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :class="!showAll ? 'btnRow' : 'unBtnRow'">
              <el-col :span="6" class="elrow" style="margin-right: 135px">
                <el-form-item prop="startTime">
                  <p>开始时间:</p>
                  <el-date-picker
                    style="width: 300px"
                    v-model="form.startTime"
                    type="datetime"
                    placeholder="选择开始时间"
                    value-format="yyyy-MM-dd HH:mm:ss"
                    format="yyyy-MM-dd HH:mm:ss"
                  ></el-date-picker>
                </el-form-item>
              </el-col>
              <el-col :span="6" class="elrow">
                <el-form-item prop="endTime">
                  <p>结束时间:</p>
                  <el-date-picker
                    style="width: 300px"
                    v-model="form.endTime"
                    type="datetime"
                    placeholder="选择结束时间"
                    value-format="yyyy-MM-dd HH:mm:ss"
                    format="yyyy-MM-dd HH:mm:ss"
                  ></el-date-picker>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </el-row>
      </div>
      <div class="formRight">
        <el-button type="primary" @click="getTableData">查询 </el-button>
        <el-button @click="handleClear">重置 </el-button>
        <el-row>
          <el-col :span="1.5" class="elrowBtn">
            <el-button type="text" size="max" @click="closeSearch">
              {{ word }}
              <i
                :class="showAll ? 'el-icon-arrow-down ' : 'el-icon-arrow-up'"
              ></i>
            </el-button>
          </el-col>
        </el-row>
      </div>
      <!-- 广告位名、广告名称、开始时间、结束时间 -->
    </div>
    <div class="advPosPage">
      <!-- <div class="form">
         广告位名、广告名称、开始时间、结束时间
        <el-form :model="form" ref="form" :inline="true" label-width="100px">
          <el-form-item label="广告位名:" prop="spaceName">
            <el-input
              style="width: 300px"
              v-model="form.spaceName"
              placeholder="请输入广告位名"
            ></el-input>
          </el-form-item>
          <el-form-item label="广告名称:" prop="adName">
            <el-input
              style="width: 300px"
              v-model="form.adName"
              placeholder="请输入广告名称:"
            ></el-input>
          </el-form-item>
          <el-form-item label="开始时间:" prop="startTime">
            <el-date-picker
              style="width: 300px"
              v-model="form.startTime"
              type="datetime"
              placeholder="选择开始时间"
              value-format="yyyy-MM-dd HH:mm:ss"
              format="yyyy-MM-dd HH:mm:ss"
            ></el-date-picker>
          </el-form-item>
          <el-form-item label="结束时间:" prop="endTime">
            <el-date-picker
              style="width: 300px"
              v-model="form.endTime"
              type="datetime"
              placeholder="选择结束时间"
              value-format="yyyy-MM-dd HH:mm:ss"
              format="yyyy-MM-dd HH:mm:ss"
            ></el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              @click="getTableData"
              style="color: #fff; margin-left: 100px; font-weight: bold"
              ><i style="margin-right: 6px" class="el-icon-search"></i>搜索
            </el-button>
            <el-button type="danger" @click="handleClear"
              ><i style="margin-right: 6px" class="el-icon-delete"></i>清空
            </el-button>
          </el-form-item>
        </el-form>
      </div> -->
      <!-- /.form -->
      <div class="table">
        <el-table
          :data="tableData"
          style="width: 100%"
          stripe
          border
          v-loading="tableLoading"
        >
          <!-- 序号 -->
          <el-table-column
            align="center"
            type="index"
            label="排序"
            min-width="60"
          ></el-table-column>
          <el-table-column
            prop="adName"
            label="广告名称"
            align="center"
            min-width="180"
          ></el-table-column>
          <el-table-column
            prop="spaceName"
            label="广告位名"
            align="center"
            min-width="180"
          ></el-table-column>
          <el-table-column label="广告类型" align="center" prop="adType">
            <template slot-scope="scope">
              <el-tag v-if="scope.row.adType === 1" type="success">商品</el-tag>
              <el-tag v-else-if="scope.row.adType === 2" type="info"
              >店铺
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column
            prop="startTime"
            label="开始时间"
            min-width="180"
            align="center"
          ></el-table-column>
          <el-table-column
            prop="endTime"
            label="结束时间"
            min-width="180"
            align="center"
          ></el-table-column>
          <el-table-column align="center" label="广告状态" prop="enable">
            <template slot-scope="scope">
              <el-switch
                @change="handleSwitch(scope.row)"
                v-model="scope.row.enable"
                active-color="#13ce66"
                inactive-color="#ff4949"
                :active-value="1"
                :inactive-value="2"
              >
              </el-switch>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            label="广告上下架状态"
            min-width="100"
            prp="status"
          >
            <template slot-scope="scope">
              <span v-if="scope.row.status === 1">正常使用</span>
              <span v-else-if="scope.row.status === 2">广告比不足下架</span>
              <span v-else-if="scope.row.status === 3">违规下架</span>
              <span v-else>广告已下架</span>
            </template>
          </el-table-column>
          <el-table-column align="center" label="操作" min-width="180">
            <template slot-scope="scope">
              <el-button type="text" size="small" @click="handleEdit(scope.row)"
              ><i style="margin-right: 6px" class="el-icon-edit"></i>编辑
              </el-button>
              <el-button
                type="text"
                size="small"
                @click="handleDelete(scope.row)"
              ><i style="margin-right: 6px" class="el-icon-delete"></i>删除
              </el-button>
              <!-- 查看 -->
              <el-button
                type="text"
                size="small"
                @click="handleView(scope.$index, scope.row)"
              ><i class="el-icon-view" style="margin-right: 6px"></i>查看
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <!-- /.table -->
      <el-row
        type="flex"
        justify="end"
        style="margin-bottom: 20px; margin-right: 20px"
      >
        <el-pagination
          background
          style="margin-top: 20px"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        ></el-pagination>
      </el-row>
      <!-- 新增弹窗 -->
      <el-dialog title="新增广告" :visible.sync="addShow" width="50%">
        <el-form
          :model="addForm"
          ref="addForm"
          :rules="addRules"
          label-width="100px"
        >
          <el-form-item label="广告名称" prop="adName">
            <el-input
              :disabled="isShow"
              clearable
              v-model="addForm.adName"
            ></el-input>
          </el-form-item>
          <!-- 请选择广告位 -->
          <el-form-item label="广告位" prop="spaceId">
            <el-select
              :disabled="isShow"
              clearable
              v-model="addForm.spaceId"
              placeholder="请选择广告位"
            >
              <el-option
                v-for="item in spaceNameList"
                :key="item.id"
                :label="item.adSpaceName"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
          <!-- 标记 -->
          <!--          <el-form-item label="标记">-->
          <!--            <el-radio v-model="addForm.mark" label="1">手机</el-radio>-->
          <!--            <el-radio v-model="addForm.mark" label="2">电脑</el-radio>-->
          <!--          </el-form-item>-->
          <!-- 请选择广告类型 -->
          <el-form-item label="广告类型">
            <el-select
              :disabled="isShow"
              clearable
              @change="handleChange"
              v-model="addForm.adType"
              placeholder="请选择广告类型"
            >
              <el-option label="商品详情" :value="1"></el-option>
              <el-option label="店铺" :value="2"></el-option>
              <!--<el-option label="促销活动" value="促销"></el-option>-->
              <!--<el-option label="秒杀活动" value="秒杀"></el-option>-->
              <!--<el-option label="拼团" value="拼团"></el-option>-->
            </el-select>
          </el-form-item>
          <el-form-item v-if="addForm.adType" :label="`选择${label}`">
            <el-select
              :disabled="isShow"
              clearable
              v-model="addForm.adShowId"
              :placeholder="`请选择${label}`"
            >
              <el-option
                v-for="item in goodsList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
          <!--开始时间 -->
          <el-form-item label="开始时间" prop="startTime">
            <el-date-picker
              :disabled="isShow"
              type="datetime"
              clearable
              placeholder="选择日期时间"
              value-format="yyyy-MM-dd HH:mm:ss"
              v-model="addForm.startTime"
            ></el-date-picker>
          </el-form-item>
          <!--结束时间 -->
          <el-form-item label="结束时间" prop="endTime">
            <el-date-picker
              :disabled="isShow"
              type="datetime"
              clearable
              value-format="yyyy-MM-dd HH:mm:ss"
              placeholder="选择日期时间"
              v-model="addForm.endTime"
            ></el-date-picker>
          </el-form-item>
          <!-- 上传图片 -->
          <el-form-item label="上传图片">
            <el-upload
              style="display: inline"
              :disabled="isShow"
              :action="uploadUrl"
              :on-preview="handlePictureCardPreview"
              :on-remove="handleRemove"
              :on-success="handleSuccess"
              :show-file-list="false"
            >
              <el-tooltip
                effect="dark"
                content="最佳尺寸：750像素 x 370像素"
                placement="top"
              >
                <el-input
                  placeholder="最佳尺寸：750像素 x 370像素"
                  style="width: 500px; margin-top: -200px"
                  v-model="addForm.img"
                ></el-input>
              </el-tooltip>
            </el-upload>
            <el-image
              style="
                width: 100px;
                display: inline-block;
                vertical-align: middle;
                height: 36px;
              "
              :src="addForm.img"
              :preview-src-list="[addForm.img]"
              alt=""
            >
              <i class="el-icon-picture-outline"></i>
            </el-image>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="handleCancel">取 消</el-button>
          <el-button type="primary" @click="handleCommit">确 定</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<style scoped lang="scss">
.form {
  width: 98%;
  min-height: 70px;
  background: #fff;
  margin: 1%;
  display: flex;
  justify-content: space-between;
  .formLeft {
    width: 1000px;
    margin-left: 20px;
    margin-top: 20px;
    ::v-deep .el-form {
      width: 100%;
    }
    ::v-deep .el-form-item__content {
      display: flex;
      justify-content: space-between;
      align-items: center;
      p {
        display: inline-block;
        // margin-left: 50px;
        min-width: 65px;
        font-size: 14px;
      }
    }
  }
  .formRight {
    width: 250px;
    display: flex;
    justify-content: space-around;
    align-items: center;
    .elrowBtn {
      margin-left: 10px;
      margin-right: 5px;
    }
  }
  .unBtnRow {
    display: none;
  }
}
.advPosPage {
  width: 98%;
  margin-top: 1%;
  margin-bottom: 1%;
  margin-left: 1%;
  overflow: hidden;
  background-color: #fff;

  .table {
    padding: 0 20px;
    margin-top: 20px;
  }
}
</style>
